package com.ruoyi.deal.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.deal.bo.HyDealCollectQueryBo;
import com.ruoyi.deal.domain.HyDealBrand;
import com.ruoyi.deal.domain.HyDealCategory;
import com.ruoyi.deal.domain.HyDealCollect;
import com.ruoyi.deal.domain.HyDealGoods;
import com.ruoyi.deal.mapper.HyDealCollectMapper;
import com.ruoyi.deal.service.IHyDealCollectService;
import com.ruoyi.deal.service.IHyDealGoodsService;
import com.ruoyi.deal.vo.HyDealCollectVo;
import com.ruoyi.deal.vo.HyDealGoodsVo;
import com.ruoyi.system.domain.HyUser;
import com.ruoyi.system.service.IHyUserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 收藏管理Service业务层处理
 *
 * @author lixin
 * @date 2021-07-04
 */
@Service
public class HyDealCollectServiceImpl extends ServicePlusImpl<HyDealCollectMapper, HyDealCollect> implements IHyDealCollectService {

	@Autowired
	private IHyUserService hyUserService;

	@Autowired
	private IHyDealGoodsService hyDealGoodsService;

	/**
	 * 查询列表
	 * @param bo 查询参数
	 * @return
	 */
    @Override
    public TableDataInfo<HyDealCollectVo> queryPageList(HyDealCollectQueryBo bo) {
        PagePlus<HyDealCollect, HyDealCollectVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HyDealCollectVo.class);

		// 返回视图数据处理
		List<HyDealCollectVo> recordsVo = result.getRecordsVo();
		for (HyDealCollectVo hyDealCollectVo : recordsVo) {

			HyUser hyUser = hyUserService.getById(hyDealCollectVo.getUserId());
			HyDealGoods goods = hyDealGoodsService.getById(hyDealCollectVo.getGoodsId());

			hyDealCollectVo.setNickName(hyUser.getNickName());
			hyDealCollectVo.setGoodsName(goods.getGoodsName());

		}
		result.setRecordsVo(recordsVo);

        return PageUtils.buildDataInfo(result);
    }

	private LambdaQueryWrapper<HyDealCollect> buildQueryWrapper(HyDealCollectQueryBo bo) {

		LambdaQueryWrapper<HyDealCollect> lqw = Wrappers.lambdaQuery();

    	if (StrUtil.isNotEmpty(bo.getNickName())) {
			List<HyUser> userList = hyUserService.queryListByNickName(bo.getNickName());
			List<Long> ids = userList.stream().map(HyUser::getUserId).collect(Collectors.toList());
			if (ids.size() > 0) {
				lqw.in(HyDealCollect::getUserId, ids);
			}
		}
        return lqw;
    }

	/**
	 * 根据用户ID查询商品收藏列表
	 * @param userId
	 * @return
	 */
	@Override
	public List<HyDealGoods> queryGoodsListByUserId(Long userId) {
		List<HyDealGoods> goodsList = new ArrayList<>();

    	List<HyDealCollect> collectList = baseMapper.selectListByUserId(userId);
		List<Integer> ids = collectList.stream().map(HyDealCollect::getGoodsId).collect(Collectors.toList());
		if (ids.size() > 0) {
			LambdaQueryWrapper<HyDealGoods> lqw = Wrappers.lambdaQuery();
			lqw.in(HyDealGoods::getGoodsId, ids);
			goodsList = hyDealGoodsService.list(lqw);
		}

		return goodsList;
	}

	@Override
	public void removeCollectByUserIdAndGoodsId(Long userId, Integer goodsId) {
		LambdaQueryWrapper<HyDealCollect> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCollect::getUserId, userId);
		lqw.eq(HyDealCollect::getGoodsId, goodsId);
		baseMapper.delete(lqw);
	}

	@Override
	public void saveCollect(Long userId, Integer goodsId) {


		if (this.isCollect(userId, goodsId)) {
			throw new CustomException("商品已在收藏列表");
		}

		HyDealCollect collect = new HyDealCollect();
		collect.setUserId(userId.intValue());
		collect.setGoodsId(goodsId);
		baseMapper.insert(collect);
	}

	/**
	 * 查询用户是否收藏
	 * @param userId
	 * @param goodsId
	 * @return
	 */
	@Override
	public Boolean isCollect(Long userId, Integer goodsId) {

		LambdaQueryWrapper<HyDealCollect> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCollect::getUserId, userId);
		lqw.eq(HyDealCollect::getGoodsId, goodsId);
		Integer count = baseMapper.selectCount(lqw);
		if (count > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 根据商品删除对应收藏记录
	 * @param goodsId
	 */
	@Override
	public void deleteByGoodsId(Integer goodsId) {
		LambdaQueryWrapper<HyDealCollect> lqw = Wrappers.lambdaQuery();
		lqw.eq(HyDealCollect::getGoodsId, goodsId);
		baseMapper.delete(lqw);
	}


}
